package com.github.tvbox.osc.ui.activity.Media

import android.content.Context
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.IdRes
import com.github.tvbox.osc.R
import com.orhanobut.logger.Logger
import io.github.source.media.bean.MediaBean

/**
 * @ClassName: MediaActionView
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: huangrongcai
 * @date: 2024/8/22
 *
 * @Copyright: 黄嵘才（HuangRc)
 */
class MediaActionView {
    private var mView: View? = null

    private var mTvSort: TextView? = null
    private var mTvQuickSearch: TextView? = null
    private var mTvCollect: TextView? = null
    private var mTvChange: TextView? = null


    companion object {
        const val ACTION_SETTING: Int = 2

        const val ACTION_SHARE: Int = 3

        const val ACTION_QUICK_SEARCH: Int = 4

        const val ACTION_COLLECT: Int = 1
        const val ACTION_SITE: Int = 0

        @JvmStatic
        public fun create(view: View?): MediaActionView {
            return MediaActionView(view)
        }

    }

    constructor(view: View?) {
        mView = view
        mTvSort = findViewById<TextView>(R.id.skin_details_Sort)
        mTvCollect = findViewById<TextView>(R.id.skin_details_Collect)
        mTvQuickSearch = findViewById<TextView>(R.id.skin_details_QuickSearch)
        mTvChange = findViewById<TextView>(R.id.skin_details_Change)
        setupActionView()
    }

    private fun setupActionView() {
        setupSort()
        setupCollect()
        setupChange()
        setupQuickSearch()
    }

    fun setupSort() {
        if (mTvSort == null) {
            return
        }
        mTvSort?.setOnClickListener(View.OnClickListener { v ->
            //变更顺序
            dispatchAction(v, 4)
        })
    }

    fun setupQuickSearch() {
        if (mTvQuickSearch == null) {
            return
        }
        mTvQuickSearch?.setOnClickListener(View.OnClickListener { v ->
            //变更顺序
            dispatchAction(v, 3)
        })
    }

    fun setupCollect() {
        if (mTvCollect == null) {
            return
        }
        mTvCollect?.setOnClickListener(View.OnClickListener { v -> //收
            /*RoomDataManger.insertVodCollect(sourceKey, vodInfo);*/
            Toast.makeText(context(), "已加入收藏夹", Toast.LENGTH_SHORT).show()
            dispatchAction(v, 2)
        })
    }

    fun setupChange() {
        if (mTvChange == null) {
            return
        }
        mTvChange?.setOnClickListener(View.OnClickListener { v -> //收
            /*RoomDataManger.insertVodCollect(sourceKey, vodInfo);*/
            Toast.makeText(context(), "换源", Toast.LENGTH_SHORT).show()
            dispatchAction(v, 1)
        })
    }

    private fun dispatchAction(view: View, action: Int) {
        if (onActionListener != null) {
            onActionListener!!.onAction(this@MediaActionView, action)
        }
    }

    private fun context(): Context? {
        return mView!!.context
    }


    private fun <T : View?> findViewById(@IdRes id: Int): T? {
        // 尝试从缓存获取，如果缓存中没有，则从视图树中查找并加入缓存
        return if (mView != null) {
            mView?.findViewById(id)
        } else {
            Logger.e("View is not initialized or null.")
            null
        }
    }

    fun getView(): View? {
        return mView
    }

    fun setMediaDetails(mediaBean: MediaBean?) {}

    //基本要素显示
    fun setMediaTitle() {}

    fun setMediaPoster() {}

    private var onActionListener: OnActionListener? = null

    fun setActionListener(listener: OnActionListener?) {
        onActionListener = listener
    }

    interface OnActionListener {
        fun onAction(view: MediaActionView?, action: Int)
    }
}